package com.superteam.zzl.model.RecommendFragment;

import android.util.Log;

import com.google.gson.Gson;
import com.superteam.zzl.activity.MainActivity;
import com.superteam.zzl.cans.ContentURLs;
import com.superteam.zzl.retrofit.RecommendFragment.MainRetrofit;
import com.superteam.zzl.viewmodel.RecommendFragment.StringConverterFactory;

import java.util.ArrayList;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * Created by zhang on 2017/1/9.
 */

public class LoadMainData {

    private Data data;
    public LoadMainData(Data data) {
        this.data = data;
        loadData();
        //Log.e("data","MainDataVM");
    }

    /**
     * 加载数据
     */
    private void loadData(){
        //Log.e("data","loadData");
        Retrofit retrofit = new Retrofit.Builder().
                baseUrl("http://www.qiugonglue.com/").
                addConverterFactory(new StringConverterFactory()).
                //addConverterFactory(ScalarsConverterFactory.create()).
                addConverterFactory(GsonConverterFactory.create()).
                build();
        MainRetrofit mr = retrofit.create(MainRetrofit.class);
        Call<String> call = mr.getData(ContentURLs.Recommend);
        call.enqueue(new Callback<String>() {
            @Override
            public void onResponse(Call<String> call, Response<String> response) {
                //Log.e("data","MV"+response.body().toString()+"\n");
                MainData mainData = new Gson().fromJson(response.body().toString(),MainData.class);
                //轮播
                ArrayList<MainData.BannerBean> banners = new ArrayList<MainData.BannerBean>();
                for (int i = 0; i < mainData.getBanner().size(); i++) {
                    banners.add(mainData.getBanner().get(i));
                }
                data.sendBanner(banners);

                //推荐城市
                ArrayList<MainData.HolidayBeanX.HolidayBean> holiday = new ArrayList<MainData.HolidayBeanX.HolidayBean>();
                for (int i = 0; i < mainData.getHoliday().getHoliday().size(); i++) {
                    holiday.add(mainData.getHoliday().getHoliday().get(i));
                }
                MainActivity.title1 = mainData.getHoliday().getTitle();
                MainActivity.content1 = mainData.getHoliday().getSub_title();
                data.sendCity(holiday);

                //人气推荐
                ArrayList<MainData.ItemsBean.ItemListBean> items = new ArrayList<MainData.ItemsBean.ItemListBean>();
                for (int i = 0; i < mainData.getItems().getItem_list().size(); i++) {
                    items.add(mainData.getItems().getItem_list().get(i));
                }
                MainActivity.title2 = mainData.getHoliday().getTitle();
                MainActivity.content2 = mainData.getHoliday().getSub_title();

                data.sendItems(items);

                //本地专题
                ArrayList<MainData.ZhuangtiBean> zhuantis = new ArrayList<MainData.ZhuangtiBean>();
                for (int i = 0; i < mainData.getZhuangti().size(); i++) {
                    zhuantis.add(mainData.getZhuangti().get(i));
                }
                data.sendZhuanTi(zhuantis);
            }

            @Override
            public void onFailure(Call<String> call, Throwable t) {
                Log.e("data","请求错误"+t.getMessage());
            }
        });
    }

}
